/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.blackup.action;

import br.com.barrientosjunior.blackup.MainManager;
import br.com.barrientosjunior.blackup.model.File;
import gnu.crypto.cipher.Serpent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Junior
 */
public class ExecuteRecoverEncryptedBackupAction implements Runnable{
    MainManager manager = null;
    File file = null;

    public ExecuteRecoverEncryptedBackupAction(MainManager manager, File file) {
        this.manager = manager;
        this.file = file;
    }

    public void execute() throws IOException {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Selecione o destino de recuperação do backup:");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        int retVal = fileChooser.showDialog(manager.getMainFrame(), "Selecionar");
        java.io.File recoverFile = null;
        if (retVal == 0){
                recoverFile = fileChooser.getSelectedFile();

                //String pass = JOptionPane.showInputDialog(manager.getMainFrame(), "Insira a senha do backup", "Senha", JOptionPane.QUESTION_MESSAGE);

                java.io.File fromFile = new java.io.File(file.getPathDestiny());
                java.io.File toFile = new java.io.File(recoverFile.getPath() + "\\" + fromFile.getName());

                FileInputStream from = null;
                FileOutputStream to = null;
                try {
                    from = new FileInputStream(fromFile);
                    to = new FileOutputStream(toFile);
                    int bs = 16;
                    byte[] buffer = new byte[bs];

                    Serpent ser = new Serpent();
                    Object k = ser.makeKey(file.getPass().getBytes(), 16);

                    byte[] encryptBuffer = new byte[bs];

                    int bytesRead;
                    while ((bytesRead = from.read(buffer)) != -1){
                        ser.decrypt(buffer, 0, encryptBuffer, 0, k, bs);
                        to.write(encryptBuffer, 0, bytesRead);
                    }
                } catch (InvalidKeyException ex) {
                    Logger.getLogger(ExecuteEncryptedBackupAction.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                  if (from != null)
                    try {
                      from.close();
                    } catch (IOException e) {
                      ;
                    }
                  if (to != null)
                    try {
                      to.close();
                    } catch (IOException e) {
                      ;
                    }
                }

                manager.getMainFrame().getjLabelStatusValue().setText("Recuperação terminada.");
        } else {
            manager.getMainFrame().getjLabelStatusValue().setText("Recuperação interrompida.");
        }
    }

    public void run(){
        try {
            this.execute();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(manager.getMainFrame(),
                    ex.getMessage(),
                    "Erro de reuperação de backup",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

}
